import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/req")
public class Req extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置被发送到客户端的响应的内容类型
        resp.setContentType("text/html");
        StringBuilder stringBuilder = new StringBuilder();

        // 获取协议及版本
        stringBuilder.append(req.getProtocol());
        stringBuilder.append("<br>");
        // 获取方法: 如GET POST
        stringBuilder.append(req.getMethod());
        stringBuilder.append("<br>");
        // 获取的是: /Servlet/req
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("<br>");
        // 获取的是: /Servlet
        stringBuilder.append(req.getContextPath());
        stringBuilder.append("<br>");
        // 获取QueryString 字符串(返回包含在路径后的请求 URL 中的查询字符串)
        stringBuilder.append(req.getQueryString());
        stringBuilder.append("<br>");
        // 返回一个 String 对象的枚举，包含在该请求中包含的参数的名称。
        Enumeration<String> headerNames = req.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            String value = req.getHeader(name);
            stringBuilder.append(name+" "+value);
            stringBuilder.append("<br>");
        }
        resp.getWriter().write(stringBuilder.toString());
    }
}
